﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WarehouseWebAPP.code.BLL;

namespace WarehouseWebAPP.Pages.admin.Edit_Entities
{
    public partial class EditSuppliers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ErrorLabel.Visible = false;

        }

        protected void SuppliersGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            SupplierDetailsView.PageIndex = SuppliersGridView.SelectedIndex;
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            SupplierBLL BLL = new SupplierBLL();
            String Previous = BLL.SupplierGetByNum((Guid)SupplierDetailsView.SelectedValue)[0].supplier_name;

          
                if (Previous != args.Value)
                {
                    try
                    {
                        if (BLL.SupplierGetByName(args.Value).Rows.Count > 0)
                            args.IsValid = false;
                        else
                            args.IsValid = true;
                    }
                    finally
                    {
                        //TODO: Dispose
                    }
                }
            
        }

        protected void SupplierDetailsView_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
        {
            try
            {
                Label Label = (Label)SupplierDetailsView.FindControl("SupplierLabel");
                SupplierBLL BLL = new SupplierBLL();
                Guid guid = BLL.SupplierGetByName(Label.Text)[0].supplier_num;

                ComponentBLL CBLL = new ComponentBLL();
                if (CBLL.ComponentGetBySupplier(guid).Rows.Count > 0)
                {
                    e.Cancel = true;
                    ErrorLabel.Visible = true;


                }
                LendItemBLL LBLL = new LendItemBLL();
                if (LBLL.lendItemGetBySupplier(guid).Rows.Count > 0)
                {
                    e.Cancel = true;
                    ErrorLabel.Visible = true;
                }

            }
            finally
            {
            }
        }
    }
}
